﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace 实验3_MDI窗口的使用
{
    public partial class FormBase : Form
    {
        ToolStripMenuItem m_tsMi = null;
        public FormBase()
        {
            InitializeComponent();
        }
        public FormBase(ToolStripMenuItem tsMi)
        {
            InitializeComponent();
            m_tsMi = tsMi;
            if (m_tsMi != null)
            {
                //禁用功能菜单项
                m_tsMi.Enabled = false;
                //OwnerItem获得父级菜单  Owner获得menuMain对象
                if (m_tsMi.OwnerItem.Owner.Items.Count > 0)
                {
                    //m_tsMi.OwnerItem.Owner.Items获得顶级菜单  本行获得“窗口”顶级菜单
                    ToolStripMenuItem tsWinMenu = (ToolStripMenuItem)m_tsMi.OwnerItem.Owner.Items[m_tsMi.OwnerItem.Owner.Items.Count - 1];
                    //生成子菜单
                    ToolStripMenuItem tsMiNew = new ToolStripMenuItem(m_tsMi.Text);
                    //将新生成的子菜单添加到“窗口”菜单下
                    tsWinMenu.DropDownItems.Add(tsMiNew);
                    WinMenuChecked(tsMiNew);
                    tsMiNew.Click += tsMiNew_Click;
                    //菜单Tag存储窗口对象
                    tsMiNew.Tag = this;
                    //窗口Tag存储“窗口”下与之关联的子菜单
                    this.Tag = tsMiNew;
                }
            }
        }
        // "窗口"下的选项被点击时，激活对应窗口
        void tsMiNew_Click(object sender, EventArgs e)
        {
            if (sender is ToolStripMenuItem)
            {
                ToolStripMenuItem tsMi = sender as ToolStripMenuItem;
                WinMenuChecked(tsMi);
                if (tsMi.Tag is Form)
                {
                    Form frm = tsMi.Tag as Form;
                    if (frm != null)
                    {
                        //激活窗口
                        frm.Activate();
                        frm.WindowState = FormWindowState.Normal;
                    }
                }
            }
        }
        // 选中“窗口”下的选项
        void WinMenuChecked(ToolStripMenuItem tsMi)
        {
            //本行获得“窗口”顶级菜单
            ToolStripMenuItem tsWinMenu = (ToolStripMenuItem)tsMi.OwnerItem;
            // “窗口”顶级菜单中，只选中参数tsMi菜单项，其他菜单均不选中
            //遍历容器内的子元素
            foreach (ToolStripMenuItem mi in tsWinMenu.DropDownItems)
            {
                mi.Checked = false;
            }
            tsMi.Checked = true;
        }
        // 窗口关闭时，恢复“菜单”下的选项，移除“窗口”下的选项
        private void FormBase_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (m_tsMi != null)
                m_tsMi.Enabled = true;
            //sender表示执行代码的对象
            if (sender is Form)
            {
                Form frm = sender as Form;
                if (frm.Tag is ToolStripMenuItem)
                {
                    //(ToolStripMenuItem)frm.Tag  ==== sender as ToolStripMenuItem
                    ToolStripMenuItem tsMi = (ToolStripMenuItem)frm.Tag;
                    ((ToolStripMenuItem)tsMi.OwnerItem).DropDownItems.Remove(tsMi);
                }
            }
        }
        // 窗口被激活时，“窗口”菜单对应选项选中
        private void WinActivated(object sender, EventArgs e)
        {
            WinMenuChecked((ToolStripMenuItem)this.Tag);
        }

        

        


    }
}
